﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : qchessboard.cpp --- QChessBoard
 *Auth       : yhni (QQ:393320854)
 *Create    : 2022/2/21
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2022/2/21
 *******************************************************************/
#include "qchessboard.h"

#include <QMouseEvent>
#include <QPainter>
#include <QDebug>

QChessBoard::QChessBoard(QWidget *parent) : QWidget(parent)
{
    this->setMouseTracking(true);

    m_background = QColor("#ffffff");
    m_line = QColor("#A52A2A");

    m_showChess = true;
    m_camp = true;

    m_timer = new QTimer(this);
    m_timer->setInterval(400);
    connect(m_timer, &QTimer::timeout, [=] {
        m_showChess = !m_showChess;
        this->update();
    });

    initChesses();
}

QChessBoard::~QChessBoard()
{
    clearChesses();
}

void QChessBoard::selectCamp(bool isRed)
{
    if (m_camp == isRed) return;
    m_camp = isRed;
    clearChesses();
    initChesses();
    adjust();
    this->update();
}

void QChessBoard::clearChesses()
{
    foreach(QChess *_chess, m_chesses) {
        m_chesses.remove(_chess->index());
        delete _chess;
        _chess = nullptr;
    }
}

void QChessBoard::initChesses()
{
    m_prevPos = QPoint(-1, -1);
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 9; j++) {
            m_chesses.insert((i << 8) | j, new QChess(i, j, QChess::UnKonw));
        }
    }

    m_chesses.value(0x0000)->setText(QStringLiteral("車"), QChess::Ju_1, !m_camp);
    m_chesses.value(0x0001)->setText(QStringLiteral("馬"), QChess::Ma_1, !m_camp);
    m_chesses.value(0x0002)->setText(m_camp ? QStringLiteral("象") : QStringLiteral("相"), QChess::Xiang_1, !m_camp);
    m_chesses.value(0x0003)->setText(m_camp ? QStringLiteral("士") : QStringLiteral("仕"), QChess::Shi_1, !m_camp);
    m_chesses.value(0x0004)->setText(m_camp ? QStringLiteral("将") : QStringLiteral("帅"), QChess::Jiang, !m_camp);
    m_chesses.value(0x0005)->setText(m_camp ? QStringLiteral("士") : QStringLiteral("仕"), QChess::Shi_2, !m_camp);
    m_chesses.value(0x0006)->setText(m_camp ? QStringLiteral("象") : QStringLiteral("相"), QChess::Xiang_2, !m_camp);
    m_chesses.value(0x0007)->setText(QStringLiteral("馬"), QChess::Ma_2, !m_camp);
    m_chesses.value(0x0008)->setText(QStringLiteral("車"), QChess::Ju_2, !m_camp);
    m_chesses.value(0x0201)->setText(QStringLiteral("炮"), QChess::Pao_1, !m_camp);
    m_chesses.value(0x0207)->setText(QStringLiteral("炮"), QChess::Pao_2, !m_camp);

    for (int i = 0; i < 5; i++) {
        m_chesses.value((3 << 8) | (i * 2))->setText(m_camp ? QStringLiteral("卒") : QStringLiteral("兵"), (QChess::Type)(QChess::Zu_1 + i), !m_camp);
        m_chesses.value((6 << 8) | (i * 2))->setText(m_camp ? QStringLiteral("兵") : QStringLiteral("卒"), (QChess::Type)(QChess::Zu_1 + i), m_camp);
    }

    m_chesses.value(0x0701)->setText(QStringLiteral("炮"), QChess::Pao_1, m_camp);
    m_chesses.value(0x0707)->setText(QStringLiteral("炮"), QChess::Pao_2, m_camp);
    m_chesses.value(0x0900)->setText(QStringLiteral("車"), QChess::Ju_1, m_camp);
    m_chesses.value(0x0901)->setText(QStringLiteral("馬"), QChess::Ma_1, m_camp);
    m_chesses.value(0x0902)->setText(m_camp ?QStringLiteral ("相") : QStringLiteral("象"), QChess::Xiang_1, m_camp);
    m_chesses.value(0x0903)->setText(m_camp ? QStringLiteral("仕") : QStringLiteral("士"), QChess::Shi_1, m_camp);
    m_chesses.value(0x0904)->setText(m_camp ? QStringLiteral("帅") : QStringLiteral("将"), QChess::Jiang, m_camp);
    m_chesses.value(0x0905)->setText(m_camp ? QStringLiteral("仕") : QStringLiteral("士"), QChess::Shi_2, m_camp);
    m_chesses.value(0x0906)->setText(m_camp ? QStringLiteral("相") : QStringLiteral("象"), QChess::Xiang_2, m_camp);
    m_chesses.value(0x0907)->setText(QStringLiteral("馬"), QChess::Ma_2, m_camp);
    m_chesses.value(0x0908)->setText(QStringLiteral("車"), QChess::Ju_2, m_camp);
}

QChess *QChessBoard::getSelected()
{
    QChess *selectChess = nullptr;
    foreach(QChess *_chess, m_chesses) {
        if (_chess->isSelected()) {
            selectChess = _chess;
            break;
        }
    }

    return selectChess;
}

void QChessBoard::exchangeChess(QChess *_chess, QChess *_other)
{
    quint16 _fid = _chess->index();
    quint16 _bid = _other->index();
    m_chesses.value(_bid)->setText(_chess->text(), _chess->getType(), _chess->isRed());
    m_chesses.value(_fid)->setText("");
}

bool QChessBoard::canBeat(QChess *_chess, QChess *_other)
{
    switch (_chess->getType()) {
    case QChess::Ju_1:
    case QChess::Ju_2:
    {
        int nCnt = getChessCount(_chess, _other);
        if (_chess->getPos().x() == _other->getPos().x()) {
            if (_other->getType() == QChess::UnKonw && !nCnt) {
                return true;
            }
            // 当前位置有子，且不属于同一阵营，是否可以吃
            else if (_chess->isRed() != _other->isRed() && !nCnt) {
                return true;
            }
        } else if (_chess->getPos().y() == _other->getPos().y()) {
            // 当前位置为空位--直线移动
            if (_other->getType() == QChess::UnKonw && !nCnt) {
                return true;
            }
            // 当前位置有子，且不属于同一阵营，是否可以吃
            else if (_chess->isRed() != _other->isRed() && !nCnt) {
                return true;
            }
        }
    }
        break;
    case QChess::Ma_1:
    case QChess::Ma_2:
    {
        int row = _chess->getPos().x();
        int col = _chess->getPos().y();
        QList<QPoint> movePos;
        movePos.append(QPoint(row - 2, col - 1));
        movePos.append(QPoint(row - 2, col + 1));
        movePos.append(QPoint(row + 2, col - 1));
        movePos.append(QPoint(row + 2, col + 1));

        movePos.append(QPoint(row - 1, col - 2));
        movePos.append(QPoint(row - 1, col + 2));
        movePos.append(QPoint(row + 1, col - 2));
        movePos.append(QPoint(row + 1, col + 2));
        for (int i = 0; i < movePos.size(); i++) {
            QPoint pos = movePos.at(i);
            if (pos.x() >= 0 && pos.y() >= 0) {
                if (pos == _other->getPos()) {
                    int x = pos.x() > _chess->getPos().x() ?  pos.x() - 1 : pos.x() + 1;
                    int y = pos.y() > _chess->getPos().y() ?  pos.y() - 1 : pos.y() + 1;
                    QChess *temp = m_chesses.value(((x << 8) | y) & 0xffff);
                    if (nullptr != temp && temp->getType() != QChess::UnKonw) {
                        qDebug() << "蹩脚马" << temp->text();
                        return false;
                    }
                    if (_chess->isRed() != _other->isRed()) return true;
                    else if (_other->getType() == QChess::UnKonw) return true;
                }
            }
        }
    }
        break;
    case QChess::Xiang_1:
    case QChess::Xiang_2:
    {
        int row = _chess->getPos().x();
        int col = _chess->getPos().y();
        QList<QPoint> movePos;
        movePos.append(QPoint(row - 2, col - 2));
        movePos.append(QPoint(row - 2, col + 2));
        movePos.append(QPoint(row + 2, col - 2));
        movePos.append(QPoint(row + 2, col + 2));

        for (int i = 0; i < movePos.size(); i++) {
            QPoint pos = movePos.at(i);
            if (pos.x() >= 0 && pos.y() >= 0) {
                if (pos == _other->getPos()) {
                    int x = pos.x() > _chess->getPos().x() ?  pos.x() - 1 : pos.x() + 1;
                    int y = pos.y() > _chess->getPos().y() ?  pos.y() - 1 : pos.y() + 1;
                    QChess *temp = m_chesses.value(((x << 8) | y) & 0xffff);
                    if (nullptr != temp && temp->getType() != QChess::UnKonw) {
                        qDebug() << "象心被占" << temp->text();
                        return false;
                    }
                    if (_chess->isRed() != _other->isRed()) return true;
                    else if (_other->getType() == QChess::UnKonw) return true;
                }
            }
        }
    }
        break;
    case QChess::Shi_1:
    case QChess::Shi_2:
    {
        int row = _chess->getPos().x();
        int col = _chess->getPos().y();
        QPoint center = (row > 5) ? QPoint(8, 4) : QPoint(1, 4);
        if ((3 == col || 5 == col) && (center == _other->getPos())) {
            if (_chess->isRed() != _other->isRed()) return true;
            else if (_other->getType() == QChess::UnKonw) return true;
        }
        else if (_chess->getPos() == center) {
            int _row = qAbs(center.x() - _other->getPos().x());
            int _col = qAbs(center.y() - _other->getPos().y());
            if (_row == 1 && 1 == _col) {
                if (_chess->isRed() != _other->isRed()) return true;
                else if (_other->getType() == QChess::UnKonw) return true;
            }
        }
    }
        break;
    case QChess::Jiang:
    {
        int row = qAbs(_chess->getPos().x() - _other->getPos().x());
        int col = qAbs(_chess->getPos().y() - _other->getPos().y());
        int _row = _other->getPos().x();
        int _col = _other->getPos().y();

        if (_col > 2 && _col < 6 && (_row < 3 || (_row > 6 && _row < 10))) {
            if ((0 == row && 1 == col) || (1 == row && 0 == col)) {
                // 老王是否见面
                foreach (QChess *chess, m_chesses) {
                    if (chess->getType() == QChess::Jiang && chess != _chess) {
                        if (_other->getPos().y() == chess->getPos().y()) {
                            if (getChessCount(_other, chess) == 0) return false;
                        }
                        break;
                    }
                }
                if (_chess->isRed() != _other->isRed()) return true;
                else if (_other->getType() == QChess::UnKonw) return true;
            }
        }
    }
        break;
    case QChess::Pao_1:
    case QChess::Pao_2:
    {
        // 纵向移动
        int nCnt = getChessCount(_chess, _other);
        if (_chess->getPos().x() == _other->getPos().x()) {
            if (_other->getType() == QChess::UnKonw && !nCnt) {
                return true;
            }
            // 当前位置有子，且不属于同一阵营，是否可以吃
            else if (_chess->isRed() != _other->isRed() && 1 == nCnt) {
                return true;
            }
        } else if (_chess->getPos().y() == _other->getPos().y()) {
            // 当前位置为空位--直线移动
            if (_other->getType() == QChess::UnKonw && !nCnt) {
                return true;
            }
            // 当前位置有子，且不属于同一阵营，是否可以吃
            else if (_chess->isRed() != _other->isRed() && 1 == nCnt) {
                return true;
            }
        }
    }
        break;
    case QChess::Zu_1:
    case QChess::Zu_2:
    case QChess::Zu_3:
    case QChess::Zu_4:
    case QChess::Zu_5:
    {
        int row = qAbs(_chess->getPos().x() - _other->getPos().x());
        int col = qAbs(_chess->getPos().y() - _other->getPos().y());

        if ((0 == row && 1 == col) || (1 == row && 0 == col)) {
            if (_chess->isRed() != _other->isRed()) return true;
            else if (_other->getType() == QChess::UnKonw) return true;
        }
    }
        break;
    default:
        break;
    }

    return false;
}

int QChessBoard::getChessCount(QChess *_chess, QChess *_other)
{
    int nCnt = 0;
    if (_chess->getPos().x() == _other->getPos().x()) {
        int row = (_chess->getPos().x() << 8) & 0xffff;
        int min = qMin(_chess->getPos().y(), _other->getPos().y()) + 1;
        int max = qMax(_chess->getPos().y(), _other->getPos().y());
        for (int i = min; i < max; i++) {
            quint16 index = row + i;
            QChess *temp = m_chesses.value(index);
            if (nullptr != temp && QChess::UnKonw != temp->getType()) {
                nCnt++;
            }
        }
    }
    else if (_chess->getPos().y() == _other->getPos().y()) {
        int col = _chess->getPos().y();
        int min = qMin(_chess->getPos().x(), _other->getPos().x()) + 1;
        int max = qMax(_chess->getPos().x(), _other->getPos().x());
        for (int i = min; i < max; i++) {
            quint16 index = (i << 8) | col;
            QChess *temp = m_chesses.value(index);
            if (nullptr != temp && QChess::UnKonw != temp->getType()) {
                nCnt++;
            }
        }
    }

    return nCnt;
}

void QChessBoard::adjust()
{
    foreach(QChess *_chess, m_chesses) {
        int radius = m_nRadius - 5;
        int nX = m_xOffSet + _chess->getPos().y() * m_nRadius  - radius / 2;
        int nY = m_yOffSet + _chess->getPos().x() * m_nRadius - radius / 2;
        _chess->setRect(nX, nY, radius, radius);
    }
}

void QChessBoard::resizeEvent(QResizeEvent *e)
{
    int side = qMin(this->width(), this->height());
    m_nRadius = side / 10;
    m_xOffSet = (this->width() - m_nRadius * 9) / 2 + m_nRadius / 2;
    m_yOffSet = (this->height() - m_nRadius * 10) / 2 + m_nRadius / 2;
    adjust();
    QWidget::resizeEvent(e);
}

void QChessBoard::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.fillRect(this->rect(), m_background);
    painter.save();

    QPen pen;
    pen.setColor(m_line);
    pen.setWidth(4);
    painter.setPen(pen);

    // 外边框
    int padding = 8;
    painter.drawLine(m_xOffSet - padding, m_yOffSet - padding, m_xOffSet + m_nRadius * 8 + padding, m_yOffSet - padding);
    painter.drawLine(m_xOffSet - padding, m_yOffSet - padding, m_xOffSet - padding, m_yOffSet + m_nRadius * 9 + padding);
    painter.drawLine(m_xOffSet - padding, m_yOffSet + m_nRadius * 9 + padding, m_xOffSet + m_nRadius * 8 + padding, m_yOffSet + m_nRadius * 9 + padding);
    painter.drawLine(m_xOffSet + m_nRadius * 8 + padding, m_yOffSet - padding, m_xOffSet + m_nRadius * 8 + padding, m_yOffSet + m_nRadius * 9 + padding);

    // 纵横线
    pen.setWidth(1);
    painter.setPen(pen);
    painter.drawLine(m_xOffSet, m_yOffSet, m_xOffSet + m_nRadius * 8, m_yOffSet);
    painter.drawLine(m_xOffSet, m_yOffSet, m_xOffSet, m_yOffSet + m_nRadius * 9);
    painter.drawLine(m_xOffSet, m_yOffSet + m_nRadius * 9, m_xOffSet + m_nRadius * 8, m_yOffSet + m_nRadius * 9);
    painter.drawLine(m_xOffSet + m_nRadius * 8, m_yOffSet, m_xOffSet + m_nRadius * 8, m_yOffSet + m_nRadius * 9);

    for(int i = 1; i < 9; i++)
        painter.drawLine(QPoint(m_xOffSet, m_yOffSet + i * m_nRadius), QPoint(m_xOffSet + 8 * m_nRadius, m_yOffSet + i * m_nRadius));

    for (int i = 1; i < 8; i++)
    {
        painter.drawLine(QPoint(m_xOffSet + i * m_nRadius, m_yOffSet), QPoint(m_xOffSet + i * m_nRadius, m_yOffSet + 4 * m_nRadius));
        painter.drawLine(QPoint(m_xOffSet + i * m_nRadius, m_yOffSet + 5 * m_nRadius), QPoint(m_xOffSet + i * m_nRadius, m_yOffSet + 9 * m_nRadius));
    }
    // 将帅斜线
    painter.drawLine(QPoint(m_xOffSet + 3 * m_nRadius, m_yOffSet), QPoint(m_xOffSet + 5 * m_nRadius, m_yOffSet + 2 * m_nRadius));
    painter.drawLine(QPoint(m_xOffSet + 3 * m_nRadius, m_yOffSet + 2 * m_nRadius), QPoint(m_xOffSet + 5 * m_nRadius, m_yOffSet));
    painter.drawLine(QPoint(m_xOffSet + 3 * m_nRadius, m_yOffSet + 7 * m_nRadius), QPoint(m_xOffSet + 5 * m_nRadius, m_yOffSet + 9 * m_nRadius));
    painter.drawLine(QPoint(m_xOffSet + 3 * m_nRadius, m_yOffSet + 9 * m_nRadius), QPoint(m_xOffSet + 5 * m_nRadius, m_yOffSet + 7 * m_nRadius));

    // 炮兵坐标
    pen.setWidth(2);
    painter.setPen(pen);
    drawPosLine(&painter, QPoint(m_xOffSet + m_nRadius, m_yOffSet + m_nRadius * 2));
    drawPosLine(&painter, QPoint(m_xOffSet + m_nRadius, m_yOffSet + m_nRadius * 7));
    drawPosLine(&painter, QPoint(m_xOffSet + m_nRadius * 7, m_yOffSet + m_nRadius * 2));
    drawPosLine(&painter, QPoint(m_xOffSet + m_nRadius * 7, m_yOffSet + m_nRadius * 7));
    for (int i = 0; i < 5; i++) {
        drawPosLine(&painter, QPoint(m_xOffSet + (i * 2) * m_nRadius, m_yOffSet + m_nRadius * 3));
        drawPosLine(&painter, QPoint(m_xOffSet + (i * 2) * m_nRadius, m_yOffSet + m_nRadius * 6));
    }
    painter.restore();

    QRect rectText(m_xOffSet + m_nRadius, m_yOffSet + 4 *m_nRadius, m_nRadius, m_nRadius);
    painter.save();
    painter.setPen(QColor("#000000"));
    painter.setFont(QFont(QStringLiteral("隶书"), m_nRadius / 2));
    painter.drawText(rectText, Qt::AlignCenter, QStringLiteral("楚"));
    rectText = QRect(rectText.right(), rectText.top(), rectText.width(), rectText.height());
    painter.drawText(rectText, Qt::AlignCenter, QStringLiteral("河"));

    rectText = QRect(rectText.right() + m_nRadius * 2, rectText.top(), rectText.width(), rectText.height());
    painter.translate(rectText.center());
    painter.rotate(-180);
    painter.drawText(QRect(-m_nRadius / 2, -m_nRadius / 2, m_nRadius, m_nRadius), Qt::AlignCenter, QStringLiteral("汉"));
    rectText = QRect(rectText.right(), rectText.top(), rectText.width(), rectText.height());
    painter.drawText(QRect(-m_nRadius / 2 - m_nRadius, -m_nRadius / 2, m_nRadius, m_nRadius), Qt::AlignCenter, QStringLiteral("界"));
    painter.restore();

    foreach(QChess *_chess, m_chesses) {
        if (QChess::UnKonw != _chess->getType()) {
            drawChess(&painter, _chess);
        }
#if 0
        else {
            int nX = m_nRadius * _chess->getPos().y() + m_xOffSet;
            int nY = m_nRadius * _chess->getPos().x() + m_yOffSet;
            painter.drawText(QPointF(nX, nY), QString("(%1,%2)").arg(_chess->getPos().x()).arg(_chess->getPos().y()));
        }
#endif
    }
}

// 绘制炮兵坐标线
void QChessBoard::drawPosLine(QPainter *painter, QPoint _pos)
{
    int space = 5;
    int offset = m_nRadius * 0.2;
    int nX = _pos.x();
    int nY = _pos.y();
    if (nX > m_xOffSet) {
        painter->drawLine(nX - space - offset, nY - space, nX - space, nY - space);
        painter->drawLine(nX - space, nY - space, nX - space, nY - offset - space);
        painter->drawLine(nX - space - offset, nY + space, nX - space, nY + space);
        painter->drawLine(nX - space, nY + space, nX - space, nY + offset + space);
    }

    if (nX < m_xOffSet + m_nRadius * 8) {
        painter->drawLine(nX + space, nY - space, nX + space + offset, nY - space);
        painter->drawLine(nX + space, nY - space, nX + space, nY - offset - space);
        painter->drawLine(nX + space, nY + space, nX + space + offset, nY + space);
        painter->drawLine(nX + space, nY + space, nX + space, nY + offset + space);
    }
}

void QChessBoard::drawChess(QPainter *painter, QChess *_chess)
{
    int radius = m_nRadius - 2;
    int nX = m_nRadius * _chess->getPos().y() + m_xOffSet;
    int nY = m_nRadius * _chess->getPos().x() + m_yOffSet;
    if (_chess->isSelected()) {
        drawChessPos(painter, _chess->getPos());
        if (m_timer->isActive() && !m_showChess) {
            return;
        }
    }

    painter->save();
    painter->translate(nX, nY);
    QRect rectChess(-radius / 2, -radius / 2, radius, radius);
    QRadialGradient Radial(0, 0, radius, 0, 0);
    Radial.setColorAt(0, QColor("#ffffff"));
    Radial.setColorAt(0.3, _chess->isRed() ? QColor("#FEE7AA") : QColor("#C7E067"));
    Radial.setColorAt(0.7, _chess->isRed() ? QColor("#E4A241") : QColor("#A27E58"));
    Radial.setColorAt(0.8, _chess->isRed() ? QColor("#FFD3A0") : QColor("#A27E58"));
    Radial.setColorAt(1.0, _chess->isRed() ? QColor("#CC8147") : QColor("#A27E58"));

    painter->setPen(_chess->isRed() ? QColor("#CC8147") : QColor("#A27E58"));
    painter->setBrush(Radial);
    painter->drawEllipse(rectChess);
    radius -= 8;
    painter->setPen(_chess->isRed() ? QColor("#CC8147") : QColor("#A27E58"));
    painter->drawEllipse(-radius / 2, -radius / 2, radius, radius);

    painter->setPen(_chess->isRed() ? QColor("#ff0000") : QColor("#000000"));
    painter->setFont(QFont(QStringLiteral("隶书"), m_nRadius / 2));
    painter->drawText(rectChess, Qt::AlignCenter, _chess->text());
    painter->restore();
}

void QChessBoard::drawChessPos(QPainter *painter, QPoint _pos)
{
    int nX = m_nRadius * _pos.y() + m_xOffSet;
    int nY = m_nRadius * _pos.x() + m_yOffSet;
    painter->save();
    painter->setPen(Qt::red);
    int width = m_nRadius * 0.2;
    int offset = m_nRadius / 2;
    painter->drawLine(nX - offset, nY - offset, nX - offset + width, nY - offset);  // 左上
    painter->drawLine(nX - offset, nY - offset, nX - offset, nY - offset + width);  // 左上
    painter->drawLine(nX + offset, nY - offset, nX + offset - width, nY - offset);  // 右上
    painter->drawLine(nX + offset, nY - offset, nX + offset, nY - offset + width);  // 右上
    painter->drawLine(nX - offset, nY + offset, nX - offset + width, nY + offset);  // 左下
    painter->drawLine(nX - offset, nY + offset, nX - offset, nY + offset - width);  // 左下
    painter->drawLine(nX + offset, nY + offset, nX + offset - width, nY + offset);  // 右下
    painter->drawLine(nX + offset, nY + offset, nX + offset, nY + offset - width);  // 右下
    painter->restore();
}

void QChessBoard::mousePressEvent(QMouseEvent *e)
{
#if 1
    if (Qt::RightButton == e->button()) {
        static bool s_isRed = true;
        s_isRed = !s_isRed;
        selectCamp(s_isRed);
        return;
    }
#endif

    if (m_timer->isActive()) m_timer->stop();
    QChess *selectChess = getSelected();
    m_showChess = true;

    foreach(QChess *_chess, m_chesses) {
        if (_chess->rect().contains(e->pos())) {
            if (nullptr == selectChess) {
                _chess->setSelected(!_chess->isSelected());
                if (_chess->isSelected()) {
                    m_timer->start();
                }
            }
            else if (canBeat(selectChess, _chess)) {
                exchangeChess(selectChess, _chess);
            }

        } else {
            _chess->setSelected(false);
        }
    }

    this->update();
}

void QChessBoard::mouseMoveEvent(QMouseEvent *e)
{
    this->setCursor(Qt::ArrowCursor);
    foreach(QChess *_chess, m_chesses) {
        if (_chess->rect().contains(e->pos())) {
            this->setCursor(Qt::PointingHandCursor);
            break;
        }
    }
}

void QChessBoard::mouseReleaseEvent(QMouseEvent *e)
{
    foreach(QChess *_chess, m_chesses) {
        if (_chess->rect().contains(e->pos())) {
            if (_chess->getType() == QChess::UnKonw) {

            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////
QChess::QChess()
{
    m_row = 0;
    m_col = 0;
    m_text = QString("兵");
    m_isRed = true;
    m_rect = QRect(0, 0, 0, 0);
    m_type = UnKonw;
    m_selected = false;
    m_river = false;
}

QChess::QChess(int _row, int _col, Type _type) :
    m_row(_row),m_col(_col),m_text(""),m_type(_type),m_isRed(false)
{
    m_rect = QRect(0, 0, 0, 0);
    m_selected = false;
    m_river = false;
}

quint16 QChess::index() const
{
    return (m_row << 8 | m_col);
}

void QChess::setRect(int nX, int nY, int nW, int nH)
{
    m_rect = QRect(nX, nY, nW, nH);
}

QRect QChess::rect() const
{
    return m_rect;
}

QPoint QChess::getPos() const
{
    return QPoint(m_row, m_col);
}

void QChess::move(int _row, int _col)
{
    if (_row < 0 || _col < 0 || _row > 9 || _col > 9) return;
    m_row = _row;
    m_col = _col;
}

void QChess::move(QPoint _pos)
{
    m_row = _pos.x();
    m_col = _pos.y();
}

QString QChess::text() const
{
    return m_text;
}

void QChess::setText(const QString &_text, QChess::Type _type, bool _isRed)
{
    m_text = _text;
    m_type = _type;
    m_isRed = _isRed;
}

bool QChess::isRed()
{
    return m_isRed;
}

void QChess::setSelected(bool bOk)
{
    m_selected = bOk;
}

bool QChess::isSelected()
{
    return m_selected;
}

QChess::Type QChess::getType()
{
    return m_type;
}

void QChess::setType(QChess::Type _type)
{
    m_type = _type;
}

bool QChess::getRiver() const
{
    return m_river;
}

void QChess::setRiver(bool river)
{
    m_river = river;
}
